﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GLImp;
using OpenTK;
using System.Drawing;

namespace BoneImp {
	public class RegionAttachment : Attachment{
		public float x, y, scaleX, scaleY, rotation, width, height;
		float[] offset = new float[8];
		Texture texture;

		public Vector2d[] texCoords;
		public RegionAttachment(string name, Region region) : base(name, AttachmentType.ATTACHMENT_REGION) {
			this.scaleX = 1;
			this.scaleY = 1;

			texture = region.texture;

			texCoords = new Vector2d[4];
			for (int i = 0; i < 4; i++) {
				texCoords[i] = new Vector2d();
			}

			int u = region.x;
			int u2 = u + region.width;
			int v = region.y;
			int v2 = v + region.height;
			if (region.rotate) {
				texCoords[1].X = u;
				texCoords[1].Y = v2;
				texCoords[2].X = u;
				texCoords[2].Y = v;
				texCoords[3].X = u2;
				texCoords[3].Y = v;
				texCoords[0].X = u2;
				texCoords[0].Y = v2;
			} else {
				texCoords[0].X = u;
				texCoords[0].Y = v2;
				texCoords[1].X = u;
				texCoords[1].Y = v;
				texCoords[2].X = u2;
				texCoords[2].Y = v;
				texCoords[3].X = u2;
				texCoords[3].Y = v2;
			}
			texCoords[0].X /= texture.Width;
			texCoords[0].Y /= texture.Height;
			texCoords[1].X /= texture.Width;
			texCoords[1].Y /= texture.Height;
			texCoords[2].X /= texture.Width;
			texCoords[2].Y /= texture.Height;
			texCoords[3].X /= texture.Width;
			texCoords[3].Y /= texture.Height;
		}

		//No function definition found...
		//abstract RegionAttachment RegionAttachment_create (const char* name, AtlasRegion* region);

		public void updateOffset() {
			float localX2 = this.width / 2;
			float localY2 = this.height / 2;
			float localX = -localX2;
			float localY = -localY2;
			localX *= this.scaleX;
			localY *= this.scaleY;
			localX2 *= this.scaleX;
			localY2 *= this.scaleY;
			float radians = (float)(this.rotation * 3.1415926535897932385 / 180);
			float cosine = (float)Math.Cos(radians);
			float sine = (float)Math.Sin(radians);
			float localXCos = localX * cosine + this.x;
			float localXSin = localX * sine;
			float localYCos = localY * cosine + this.y;
			float localYSin = localY * sine;
			float localX2Cos = localX2 * cosine + this.x;
			float localX2Sin = localX2 * sine;
			float localY2Cos = localY2 * cosine + this.y;
			float localY2Sin = localY2 * sine;
			this.offset[0] = localXCos - localYSin;
			this.offset[1] = localYCos + localXSin;
			this.offset[2] = localXCos - localY2Sin;
			this.offset[3] = localY2Cos + localXSin;
			this.offset[4] = localX2Cos - localY2Sin;
			this.offset[5] = localY2Cos + localX2Sin;
			this.offset[6] = localX2Cos - localYSin;
			this.offset[7] = localYCos + localX2Sin;
		}

		public override void draw(Slot slot) {
			GraphicsManager.SetColor(Color.White);

			Vector2d[] vertices = new Vector2d[4];
			for (int i = 0; i < 4; i++) {
				vertices[i] = new Vector2d();
			}

			Bone bone = slot.bone;
			vertices[0].X = offset[0] * bone.m00 + offset[1] * bone.m01 + bone.worldX;
			vertices[0].Y = offset[0] * bone.m10 + offset[1] * bone.m11 + bone.worldY;
			vertices[1].X = offset[2] * bone.m00 + offset[3] * bone.m01 + bone.worldX;
			vertices[1].Y = offset[2] * bone.m10 + offset[3] * bone.m11 + bone.worldY;
			vertices[2].X = offset[4] * bone.m00 + offset[5] * bone.m01 + bone.worldX;
			vertices[2].Y = offset[4] * bone.m10 + offset[5] * bone.m11 + bone.worldY;
			vertices[3].X = offset[6] * bone.m00 + offset[7] * bone.m01 + bone.worldX;
			vertices[3].Y = offset[6] * bone.m10 + offset[7] * bone.m11 + bone.worldY;


			GraphicsManager.SetTexture(texture);
			GraphicsManager.DrawRectangle(vertices[0], vertices[1], vertices[2], vertices[3], texCoords[0], texCoords[1], texCoords[2], texCoords[3]);
		}
	}
}
